// 入口函数
$(function () {
  // 截取路径里面的id
  let id = location.search.replace("?", "");
  // 渲染页面
  initArtCateList();

  function initArtCateList() {
    $.ajax({
      url: "/api/articles/" + id + "/comments",
      method: "get",
      success: function (res) {
        console.log(res);
        var htmlStr = template("tpl-list", res);
        $(".kr_comment_card").html(htmlStr);
      },
    });
  }
  // 改变时间样式
  template.defaults.imports.dateFormat = function (dataStr) {
    var dt = new Date(dataStr);
    var y = dt.getFullYear();
    var m = dt.getMonth() + 1;
    m = padZero(m);
    var d = dt.getDate();
    d = padZero(d);

    var hh = dt.getHours();
    hh = padZero(hh);
    var mm = dt.getMinutes();
    mm = padZero(mm);
    var ss = dt.getSeconds();
    ss = padZero(ss);
    return `${y}-${m}-${d}   ${hh}:${mm}:${ss}`;
  };

  function padZero(n) {
    return n < 10 ? "0" + n : n;
  }
  // 当文本框输入信息时改变数字
  $("#xxx").on("input", function (e) {
    // e.stopPropagation()
    //   e.preventDefault();
    var number = $(this).val().length;
    console.log(number);
    var figure = `<span class="word_count">${number}/1000</span><button lay-submit id="comment-btn">提交评论</button>`;
    $("#review").html(figure);
    // console.log(1);
  });
  //提交评论
  $("#comment-form").on("submit", function (e) {
    var data = $(this).serialize();
    e.preventDefault();
    $.ajax({
      url: "/api/articles/" + id + "/comments",
      method: "post",
      data: data,
      success: function (res) {
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        var htmlStr = template("tpl-list", res);
        $(".kr_comment_card").html(htmlStr);
        document.querySelector("#comment-form").reset();
        var figures = `<span class="word_count">0/1000</span><button lay-submit id="comment-btn">提交评论</button>`;
        $("#review").html(figures);
        initArtCateList();
      },
    });
  });
  // 点赞
  // var likes = document.querySelector(".kr_body > .like");
  // var buttonAfterStyle = getComputedStyle(likes, "::before");
  // console.log(buttonAfterStyle);
  var falj = true;
  getUserInfo();
  $("#tages").on("click", function () {
    if (falj) {
      $("#tages").css("background-color", "red").css("border-radius", "100000px");
      var fabulous = 1;
      howws(fabulous);
      return (falj = false);
    } else {
      $("#tages").css("background-color", "#fff").css("border-radius", "100000px");

      var fabulous = 0;
      howws(fabulous);
      return (falj = true);
    }
  });

  // 传入点赞次数
  function howws(fabulous) {
    console.log(fabulous);
    $.ajax({
      url: "/api/fabulous/" + id,
      method: "post",
      data: {
        fabulous,
      },
      success: function (res) {
        getUserInfo();
      },
    });
  }

  // 获取点赞的次数并渲染
  function getUserInfo() {
    $.ajax({
      url: "/api/fabulous/" + id,
      method: "get",
      success: function (res) {
        console.log(res.data);
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        var htmlStr = `<div data-count="${res.data}"></div>`;
        $("#tages").html(htmlStr);
      },
    });
  }

  // 收藏
  var faljs = true;
  wwwout();
  $("#enshrine").on("click", function () {
    // console.log(1);
    if (faljs) {
      $("#enshrine").css({
        backgroundImage: "url('/bigevent-front/assets/js/collect_active.png')",
      });
      var fabulous = 1;
      sssout(fabulous);
      return (faljs = false);
    } else {
      $("#enshrine").css({
        backgroundImage: "url('/bigevent-front/assets/js/collect_pray.png')",
      });
      var fabulous = 0;
      sssout(fabulous);
      return (faljs = true);
    }
  });
  // 获取
  function wwwout() {
    $.ajax({
      url: "/api/collection/" + id,
      method: "get",
      success: function (res) {
        console.log(res);
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        if (res.data == 1) {
          $("#enshrine").css({
            backgroundImage: "url('/bigevent-front/assets/js/collect_active.png')",
          });
          faljs = false;
        }
      },
    });
  }

  function sssout(fabulous) {
    $.ajax({
      url: "/api/collection/" + id,
      method: "post",
      data: {
        status: fabulous,
      },
      success: function (res) {
        console.log(res);
        wwwout();
      },
    });
  }

  $.ajax({
    url: "/api/links",
    method: "get",
    success: function (res) {
      var html = template("tpl2", res);
      $(".kr_collaborator").append(html);
    },
  });

  $.ajax({
    url: "/api/articlecate/" + id,
    method: "get",
    success: function (res) {
      console.log(res);
      var html = template("tpl3", res);
      $(".kr_news_detail").append(html);
    },
  });
});
